/**
 * 一个基础的express 服务端程序
 * 1、集成log4js
 * 2、集成数据库操作
 * 3、集成nconf配置工具
 *
 * @type {*|exports|module.exports}
 */

var path=require('path');
var express = require('express');
var app = express();

var log4js = require('./lib/logger.js');

var logger=log4js.logger();

var CommonRouter=require('./rest/common.js');

var nconf=require('nconf').argv().env();

var bodyParser=require('body-parser');


app.set("view engine","ejs");
app.set('views', path.join(__dirname, '/views'));
app.use(express.static(__dirname+"/static"))

//配置日志中间件
log4js.use(app);

//bodyParser application/x-www-form-urlencoded  接受表单请求处理
app.use(bodyParser.urlencoded({extended:false}))

//bodyParser application/json 接受json请求
app.use(bodyParser.json())


/**
 * rest 接口路由
 */
app.use('/comm', CommonRouter);

/**
 * 访问根目录、默认输出hello world
 */
app.get('/', function (req, res) {
    res.render("index",{message:"hello world"});
});



/**
 * 启动服务
 * @type {http.Server}
 */
var server = app.listen(nconf.get('port')||3000, function () {
    var host = server.address().address;
    var port = server.address().port;
    logger.debug('Server Start : http://%s:%s', host, port);
});

module.exports=app;